<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

/**
 *Contains framework included files and application level utility functions 
 *Trigger : onLogin 
 */
/* Give the session a unique name to avoid conflicts and start the session. */
//@session_name("NANAAL_SESSION");
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (function_exists('date_default_timezone_set'))
{
    @date_default_timezone_set(date_default_timezone_get());
}
$arrPathInfo=pathinfo($_SERVER["SCRIPT_NAME"]);
define("URL_BASE_FILE_NAME", $arrPathInfo["basename"]);
define("NAANAL_CONTROL_PANEL", $arrPathInfo["filename"]);

$arrDebug=debug_backtrace();
$arrPrevFile=array_shift($arrDebug);
$prevFile=$arrPrevFile["file"];
$prevDirectory=dirname($arrPrevFile["file"])."/";
define("NAANAL_APP_PATH",$prevDirectory);
define("NAANAL_FRAMEWORK_PATH",  dirname(__FILE__)."/");

//set_time_limit(0);
include_once dirname(__FILE__).'/config.php';
if(isset($NAANAL_DEBUG) && $NAANAL_DEBUG==false)
{
    function trace($data)
    {
        var_dump($data);
    }
}
else include_once(PHPTRACE_PATH."debug.php");
include_once dirname(__FILE__)."/lib/ClsNaanalController.php";
include_once dirname(__FILE__)."/lib/ClsNaanalApplication.php";
include_once dirname(__FILE__)."/lib/ClsLDB.php";
include_once dirname(__FILE__)."/lib/ClsLDBManage.php";
include_once(dirname(__FILE__)."/lib/ClsNaanalInstall.php");
include_once(dirname(__FILE__)."/lib/ClsNaanalDownload.php");
include_once(dirname(__FILE__)."/lib/ClsNaanalEMail.php");
include_once dirname(__FILE__).'/lib/dom.php';
include_once dirname(__FILE__).'/lib/ClsNaanalCrypt.php';
include_once dirname(__FILE__).'/lib/ClsNaanalMenu.php';
include_once dirname(__FILE__).'/lib/ClsNaanalJS.php';
include_once dirname(__FILE__).'/lib/ClsNaanalConfigGen.php';
include_once dirname(__FILE__).'/lib/ClsNaanalHTML.php';
include_once dirname(__FILE__).'/lib/ClsNaanalClient.php';
if(isset($_REQUEST["trace"]))
{
    define("PHPTRACE_DISPLAY_ERROR", empty($_REQUEST["trace"])?true:$_REQUEST["trace"]);
}
/**
 * This class remembers the state of the a page.
 */
class ClsLState
{
    function __construct()
    {
    }
}
/**
 * returns js file and related css files and related image files
 * @global string $jslibspath
 * @global string $csslibpath
 * @param string $name
 * @return boolean|array 
 */
function getJSInclude($name)
{
    global $jslibspath,$csslibpath;
    if(file_exists($jslibspath.$name))
    {
        return array("js"=>$jslibspath.$name.$name.'.js',"css"=>$jslibspath.$name.$name.'.css');
    }
    return false;
}
function getTimeZoneList()
{
    /* Time Zones */
    // FIXME: Support fractional GMT offsets.
    $timeZones = array(
        array(-12,  'GMT-12:00 International Date Line West'),
        array(-11,  'GMT-11:00 Midway Island, Samoa'),
        array(-10,  'GMT-10:00 Hawaii'),
        array(-9,   'GMT-09:00 Alaska'),
        array(-8,   'GMT-08:00 Pacific Time (US and Canada)'),
        array(-8,   'GMT-08:00 Tijuana, Baja California'),
        array(-7,   'GMT-07:00 Mountain Time (US and Canada)'),
        array(-7,   'GMT-07:00 Arizona'),
        array(-7,   'GMT-07:00 Chihuahua, La Paz, Mazatlan - New'),
        array(-7,   'GMT-07:00 Chihuahua, La Paz, Mazatlan - Old'),
        array(-6,   'GMT-06:00 Central Time (US and Canada)'),
        array(-6,   'GMT-06:00 Central America'),
        array(-6,   'GMT-06:00 Guadalajara, Mexico City, Monterrey - New'),
        array(-6,   'GMT-06:00 Guadalajara, Mexico City, Monterrey - Old'),
        array(-6,   'GMT-06:00 Saskatchewan'),
        array(-5,   'GMT-05:00 Eastern Time (US and Canada)'),
        array(-5,   'GMT-05:00 Bogota, Lima, Quito, Rio Branco'),
        array(-5,   'GMT-05:00 Indiana (East)'),
        array(-4,   'GMT-04:00 Atlantic Time (Canada)'),
        array(-4,   'GMT-04:00 Caracas, La Paz'),
        array(-4,   'GMT-04:00 Manaus'),
        array(-4,   'GMT-04:00 Santiago'),
      //array(-3.5, 'GMT-03:30 Newfoundland'),
        array(-3,   'GMT-03:00 Greenland'),
        array(-3,   'GMT-03:00 Brasilia'),
        array(-3,   'GMT-03:00 Buenos Aires, Georgetown'),
        array(-3,   'GMT-03:00 Montevideo'),
        array(-2,   'GMT-02:00 Mid-Atlantic'),
        array(-1,   'GMT-01:00 Azores'),
        array(-1,   'GMT-01:00 Cape Verde Is.'),
        array(0,    'GMT Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London'),
        array(0,    'GMT Casablanca, Monrovia, Reykjavik'),
        array(1,    'GMT+01:00 Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna'),
        array(1,    'GMT+01:00 Belgrade, Bratislava, Budapest, Ljubljana, Prague'),
        array(1,    'GMT+01:00 Brussels, Copenhagen, Madrid, Paris'),
        array(1,    'GMT+01:00 Sarajevo, Skopje, Warsaw, Zagreb'),
        array(1,    'GMT+01:00 West Central Africa'),
        array(2,    'GMT+02:00 Amman'),
        array(2,    'GMT+02:00 Athens, Bucharest, Istanbul'),
        array(2,    'GMT+02:00 Beirut'),
        array(2,    'GMT+02:00 Cairo'),
        array(2,    'GMT+02:00 Harare, Pretoria'),
        array(2,    'GMT+02:00 Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius'),
        array(2,    'GMT+02:00 Jerusalem'),
        array(2,    'GMT+02:00 Minsk'),
        array(2,    'GMT+02:00 Windhoek'),
        array(3,    'GMT+03:00 Baghdad'),
        array(3,    'GMT+03:00 Kuwait, Riyadh'),
        array(3,    'GMT+03:00 Moscow, St. Petersburg, Volgograd'),
        array(3,    'GMT+03:00 Nairobi'),
        array(3,    'GMT+03:00 Tbilisi'),
      //array(3.5,  'GMT+03:30 Tehran'),
        array(4,    'GMT+04:00 Abu Dhabi, Muscat'),
        array(4,    'GMT+04:00 Baku'),
        array(4,    'GMT+04:00 Yerevan'),
      //array(4.5,  'GMT+04:30 Kabul'),
        array(5,    'GMT+05:00 Ekaterinburg'),
        array(5,    'GMT+05:00 Islamabad, Karachi, Tashkent'),
      array(5.5,  'GMT+05:30 Chennai, Kolkata, Mumbai, New Delhi'),
      //array(5.5,  'GMT+05:30 Sri Jayawardenepura'),
      //array(5.75, 'GMT+05:45 Kathmandu'),
        array(6,    'GMT+06:00 Almaty, Novosibirsk'),
        array(6,    'GMT+06:00 Astana, Dhaka'),
      //array(6.5,  'GMT+06:30 Yangon (Rangoon)'),
        array(7,    'GMT+07:00 Bangkok, Hanoi, Jakarta'),
        array(7,    'GMT+07:00 Krasnoyarsk'),
        array(8,    'GMT+08:00 Beijing, Chongqing, Hong Kong, Urumqi'),
        array(8,    'GMT+08:00 Irkutsk, Ulaan Bataar'),
        array(8,    'GMT+08:00 Kuala Lumpur, Singapore'),
        array(8,    'GMT+08:00 Perth'),
        array(8,    'GMT+08:00 Taipei'),
        array(9,    'GMT+09:00 Osaka, Sapporo, Tokyo'),
        array(9,    'GMT+09:00 Seoul'),
        array(9,    'GMT+09:00 Yakutsk'),
      //array(9.5,  'GMT+09:30 Adelaide'),
      //array(9.5,  'GMT+09:30 Darwin'),
        array(10,   'GMT+10:00 Brisbane'),
        array(10,   'GMT+10:00 Canberra, Melbourne, Sydney'),
        array(10,   'GMT+10:00 Guam, Port Moresby'),
        array(10,   'GMT+10:00 Hobart'),
        array(10,   'GMT+10:00 Vladivostok'),
        array(11,   'GMT+11:00 Magadan, Solomon Is., New Caledonia'),
        array(12,   'GMT+12:00 Auckland, Wellington'),
        array(12,   'GMT+12:00 Fiji, Kamchatka, Marshall Is.'),
        array(12,   'GMT+13:00 Nuku`alofa')
    );
    return $timeZones;
}
function getNaanalLibObject()
{
    $args=func_get_args();
    $class=  array_shift($args);
    if(!class_exists($class))
    {
        if(file_exists(NAANAL_PATH_LIB.$class.".php"))
        {
            include_once(NAANAL_PATH_LIB.$class.".php");
        }
        else
            return null;
    }
    if($args)
    {
        $arrArg=array();
        foreach($args as $ind=>$arg)
        {
            $arrArg["key{$ind}"]=$arg;
        }
        extract($arrArg);
        $class=$class.'(';
        foreach($args as $ind=>$arg)
        {
            if($ind===0)
            {
                $class=$class."\$key{$ind}";
            }
            else
            {
                $class=$class.",\$key{$ind}";
            }
        }
        $class=$class.')';
    }
    eval('$obj=new '.$class.';');
    return $obj;
}
function getCurrentControlPanel()
{
    $objNaanalSession=ClsNaanalSession::getInstance();
    if($objNaanalSession->isDataSet("loginpanel"))
    {
        return $objNaanalSession->getData("loginpanel");
    }
    else 
    {
        return "guest";
    }
}
function getLoginControlPanel()
{
    if(NAANAL_CONTROL_PANEL=="index") return "user";
    else return NAANAL_CONTROL_PANEL;
}
/**
* 
* if param $contolPanel is set, this function checks whether the user logged in at this control panel
* else the function checks whether the user logged in current control panel
* @param $controlPanel
*/
function isLogged($naanalPanel="")
{
    $is_logged=getHook("is_logged");
    if($is_logged)
    {
        return $is_logged();
    }
    else
    {
        $objNaaanalSession=ClsNaanalSession::getInstance();
        if($objNaaanalSession->getLoginID()>0) return true;
        return false;
    }
}
function getLoginTime()
{
    $objNaaanalSession=ClsNaanalSession::getInstance();
    return $objNaaanalSession->getPanelData("NAANAL_LOGIN_TIME");
}
/**
 * Get dynamic menu caption.
 * @param string $type
 * @return string 
 */
function getMenuCaption($type)
{
    if($type=="logout")
    {
        if($tmp=getCurrentUser())
        {
            return "Logout";
        }
        else
            return "Login";
    }
    else if($type=="register")
    {
        if($tmp=  ClsNaanalApplication::getCurrentUser())
        {
            return "";
        }
        else
            return "Register";
    }
    else return $type; 
}


/*function __autoload($class)
{
    ClsNaanalApplication::loadClass($class);
}*/
ClsNaanalApplication::loadNaanalApp();

?>